package qdu.rg012.myrecyclerview;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.lang.invoke.LambdaConversionException;
import java.util.List;

import qdu.rg012.myrecyclerview.Model.BookBean;

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyViewHolder> {

    List<BookBean> booklist;
    String [] urls;
    Context context;
    public MyRecyclerAdapter(List<BookBean> booklist) {
        this.booklist = booklist;
    }

    public MyRecyclerAdapter(String[] urls, Context context) {
        this.urls = urls;
        this.context = context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //该函数负责创建布局对象，并将其送入自定义的Ui元素保存器对象
        //在保存器对象中，将对应的UI元素id取出并保存
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ly_itemview, parent,false);
        MyViewHolder myholder = new MyViewHolder(view);
        return myholder;
    }

    @Override
    public int getItemCount() {
        //return booklist.size();
        return urls.length;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        //该函数根据position确定了数据源中的数据，将其分配到参数一指定的UI元素中
//        if(booklist != null){
//            holder.bookname.setText(booklist.get(position).getBookname());
//
//            holder.bookimg.setImageResource(booklist.get(position).getImgid());
//        }
        if(urls.length != 0){
            Log.i("mytag", "url:"+urls[position]);
            Glide.with(context.getApplicationContext()).load(urls[position])
                    .timeout(8000).into(holder.bookimg);
        }
    }

//    static class MyViewHolder extends RecyclerView.ViewHolder{
//        ImageView bookimg;
//        TextView bookname;
//        public MyViewHolder(@NonNull View itemView) {
//            super(itemView);
//            bookimg = (ImageView) itemView.findViewById(R.id.iv_bookimg);
//            bookname = (TextView) itemView.findViewById(R.id.tv_bookname);
//        }
//    }
}
